package vnt.farming;

import java.util.ArrayList;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.widget.Toast;

public class Database {
	private final String DBNAME = "farming.db";
	private SQLiteDatabase database = null;
	
	public Database()
	{
		database = SQLiteDatabase.openOrCreateDatabase(DBNAME, null);
	}
	
	public ArrayList<TblThuoc> getAll()
	{
		ArrayList<TblThuoc> tblThuocs = new ArrayList<TblThuoc>();
		Cursor c = database.query("TblThuoc", null, null, null, null, null, null);
		c.moveToFirst();
		TblThuoc tblThuoc;
		String data="";
		while(c.isAfterLast()==false)
		{
			tblThuoc = new TblThuoc();
			tblThuoc.setMaSo(c.getInt(0));
			tblThuoc.setTenThuoc(c.getString(1));
			tblThuoc.setHoatChat(c.getString(2));
			tblThuoc.setDoiTuong(c.getString(3));
			tblThuoc.setGhiChu(c.getString(4));
			tblThuoc.setCachLy(c.getString(5));
			tblThuoc.setGocThuoc(c.getString(6));
			tblThuoc.setCongTy(c.getString(7));
			
			tblThuocs.add(tblThuoc);
			c.moveToNext();
		}
		c.close();
		return tblThuocs;
	}

	public TblThuoc getById(int pId)
	{
		TblThuoc tblThuoc = null;
		Cursor c = database.query("TblThuoc", null, "MaSo=" + pId, null, null, null, null);
		if(c != null)
		{
			if(c.moveToFirst())
			{
				tblThuoc = new TblThuoc();
				tblThuoc.setMaSo(c.getInt(0));
				tblThuoc.setTenThuoc(c.getString(1));
				tblThuoc.setHoatChat(c.getString(2));
				tblThuoc.setDoiTuong(c.getString(3));
				tblThuoc.setGhiChu(c.getString(4));
				tblThuoc.setCachLy(c.getString(5));
				tblThuoc.setGocThuoc(c.getString(6));
				tblThuoc.setCongTy(c.getString(7));
			}
			c.close();
		}
		return tblThuoc;
	}
}
